import Vue from 'vue'
import Vuex from 'vuex'
// 导入本地存储的模块
import { setItem, getItem, removeItem } from '@/assets/utils/storeage'

Vue.use(Vuex)

const tokenKey = 'user'

export default new Vuex.Store({
  state: {
    // 一个对象用于存储当前登录用户信息里面包含token等数据
    user: getItem(tokenKey)
  },
  mutations: {
    saveToken(state, data) {
      state.user = data
      // 为了防止刷新丢失需要把数据备份到本地存储
      setItem(tokenKey, state.user)
    },
    removeToken(state) {
      state.user = null
      removeItem(tokenKey)
    }
  },
  actions: {},
  modules: {}
})
